<template>
    <div class="app-container">
        <div class="filter-container">
            <el-input v-model="listQuery.name" placeholder="名字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
            <productAttrCateSelect v-model="listQuery.productAttributeCategoryId" placeholder="选择分类"></productAttrCateSelect>
            <appSelect v-model="listQuery.appId" placeholder="所属应用"></appSelect>
            <companySelect v-model="listQuery.company" placeholder="所属商户"></companySelect>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                查询
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
                新增
            </el-button>
        </div>

        <el-table
            :key="tableKey"
            v-loading="listLoading"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%;"
            @sort-change="sortChange"
        >

            <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
                <template slot-scope="{row}">
                    <span>{{ row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column label="商品属性分类" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.productAttributeCategoryName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="属性名" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column label="属性录入方式" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.inputType == 0 ? '手工录入' : '从列表中选取' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="可选值列表" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.inputList }}</span>
                </template>
            </el-table-column>
            <el-table-column label="排序" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.orderNum }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所属商户" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.companyName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所属应用" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.appName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.createTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.updateTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="{row,$index}">
                    <el-button type="primary" size="mini" @click="handleUpdate(row)">
                        编辑
                    </el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
                <el-form-item label="属性分类">
                  <productAttrCateSelect v-model="temp.productAttributeCategoryId" placeholder="选择分类"></productAttrCateSelect>
                </el-form-item>
                <el-form-item label="属性名">
                    <el-input v-model="temp.name" />
                </el-form-item>
                <el-form-item label="属性录入方式">
                  <el-select v-model="temp.inputType" class="filter-item" clearable placeholder="选择录入方式">
                    <el-option v-for="item in [{id:0,name:'手工录入'},{id:1,name:'从列表选取'}]" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
                <el-form-item label="可选值列表">
                    <el-input v-model="temp.inputList" /><span style="position: absolute;right: -80px;font-size: small;font-weight: 300">以逗号隔开</span>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="temp.orderNum" />
                </el-form-item>
                <el-form-item label="所属商户">
                    <company-select v-model="temp.company"></company-select>
                </el-form-item>
                <el-form-item label="所属应用">
                    <app-select v-model="temp.appId"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">
                    取消
                </el-button>
                <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
                    确定
                </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import {
        productAttributeList,
        productAttributeCreate,
        productAttributeUpdate,
        productAttributeDelete
    } from '@/api/productAttribute'
    import waves from '@/directive/waves'
    import Pagination from '@/components/Pagination'
    import appSelect from '@/views/app/components/appSelect'
    import companySelect from '@/views/app/components/companySelect'
    import productAttrCateSelect from '@/views/business/components/productAttrCateSelect'

    const defaultListQuery = {
        page: 1,
        limit: 15,
        name: '',
        company: '',
        productAttributeCategoryId: undefined,
        appId:'',
        sort: '+id'
    };

    const currentTemp = {
        id: undefined,
        productAttributeCategoryId: undefined,
        name: undefined,
        selectType: undefined,
        inputType: undefined,
        inputList: undefined,
        orderNum: undefined,
        company: undefined,
        appId: undefined,
        createTime: undefined,
        updateTime: undefined,
    }

    export default {
        name: 'productAttribute',
        components: { Pagination, appSelect, companySelect, productAttrCateSelect },
        directives: { waves },
        filters: {

        },
        data() {
            // var name = (rule, value, callback) => {
            //   if (value === '') {
            //     callback(new Error('模拟后台校验，请输入用户名'));
            //   } else {
            //     callback();
            //   }
            // }
            return {
                tableKey: 0,
                list: null,
                total: 0,
                listLoading: true,
                listQuery: Object.assign({}, defaultListQuery),
                temp: Object.assign({}, currentTemp),
                dialogFormVisible: false,
                dialogStatus: '',
                textMap: {
                    update: '修改',
                    create: '新增'
                },
                rules: {
                    // name: [{ validator: name, trigger: 'blur' }]
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                this.listLoading = true
                productAttributeList(this.listQuery).then(response => {
                    this.list = response.data.content
                    this.total = parseInt(response.data.totalElements)

                    this.listLoading = false
                })
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            sortChange(data) {
                const { prop, order } = data
                if (prop === 'id') {
                    this.sortByID(order)
                }
            },
            sortByID(order) {
                if (order === 'ascending') {
                    this.listQuery.sort = '+id'
                } else {
                    this.listQuery.sort = '-id'
                }
                this.handleFilter()
            },
            resetTemp() {
                this.temp = Object.assign({}, currentTemp)
            },
            handleCreate() {
                this.resetTemp()
              this.temp.productCategoryId = '3000217'
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })

            },
            createData() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        productAttributeCreate(this.temp).then(() => {
                            this.list.unshift(this.temp)
                            this.dialogFormVisible = false
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.getList()
                        })
                    }
                })
            },
            handleUpdate(row) {
                this.temp = Object.assign({}, row) // copy obj
                this.dialogStatus = 'update'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })

            },
            updateData() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        const tempData = Object.assign({}, this.temp)
                        tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
                        productAttributeUpdate(tempData).then(() => {
                            const index = this.list.findIndex(v => v.id === this.temp.id)
                            this.list.splice(index, 1, this.temp)
                            this.dialogFormVisible = false
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.getList()
                        })
                    }
                })
            },
            handleDelete(row, index) {
                const tempData = {};
                tempData.id = row.id;
                const that = this;
                this.$confirm('确认删除？', '系统提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    cancelButtonClass: 'btn-custom-cancel',
                    type: 'warning'
                }).then(function(){
                    productAttributeDelete(tempData).then(() => {
                        that.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        })
                        that.getList()
                    })
                }).catch(function(){
                    // console.log("取消成功！");
                })
            }
        }
    }
</script>

<style lang="scss" scoped>


</style>
